<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import {
		useRoute
	} from 'vue-router'
	import {
		navigateBack
	} from "@/assets/scripts/navigate.js"
	import {
		deleteWishTreeUserApi,
		getWishTreeUserListApi,
	} from "@/assets/api/other/wishingTree.js"

	const sheetList = ref([{
		name: '删除',
		subname: "删除该许愿",
		color: '#ffaa7f',
		fontSize: '20'
	}])
	const userInfo = ref({})
	const actionSheet = ref()
	const list = ref([])
	const pageNum = ref(1)
	const total = ref(10)
	const loading = ref(true)
	const itemId = ref('')

	const handleDel = async () => {
		try {
			const row = list.value.find(o => o.id === itemId.value)
			const res = await deleteWishTreeUserApi(itemId.value)
			if (res && res.code === 200) {
				uni.showToast({
					title: '删除成功',
					icon: 'success',
					success: () => {
						list_init() // 请求记录数据
					}
				})
			} else throw new Error('删除失败')
		} catch (e) {
			uni.showToast({
				title: e?.message || '操作失败',
				icon: 'error'
			})
		}
	}


	const handleSelect = (e) => {
		console.log('选中该项：', e);
		if (e.name === '删除') {
			handleDel()
		}
	}

	const handleOpt = (item) => {
		console.log(item);
		itemId.value = item.id
		actionSheet.value.open()
	}

	onMounted(() => {
		userInfo.value = uni.getStorageSync('user')
		list_init() // 请求记录数据
	})

	const handleMore = () => {
		if (loading.value) return
		loading.value = true
		pageNum.value = pageNum.value + 1
		list_init() // 请求记录数据
	}

	const list_init = async () => {
		const res = await getWishTreeUserListApi(userInfo.value.userId, '', '', pageNum.value)
		if (res && res.rows) {
			total.value = res.total
			const rows = res.total <= (pageNum.value - 1) * 10 ? [] : res.rows
			list.value = [...list.value.slice(0, (pageNum.value - 1) * 10), ...rows]
			loading.value = false
		}
	}
</script>

<template>
	<view class="">
		<uv-navbar leftText="返回" :fixed="false" left-icon-color="#fff" bgColor="#e15c19" @leftClick="navigateBack">
			<template v-slot:right>
				<view class="uv-nav-slot">
					您有 ({{total}}) 个愿望签
				</view>
			</template>
		</uv-navbar>

		<uv-action-sheet ref="actionSheet" :actions="sheetList" title="请选择操作" @select="handleSelect">
		</uv-action-sheet>

		<view class="mt-normal px-normal" v-for="(item, index) in list" :key="index">
			<view class="item">
				<view class="d-flex d-flex-jcsb d-flex-aic">
					<view class="">
						{{item.wtName}}
					</view>
					<view class="item-more" @click='handleOpt(item)'>
						<uv-icon name="more-dot-fill" color="#333"></uv-icon>
					</view>
				</view>
				<view class="item-time">
					{{item.createTime}}
				</view>
				<view class="item-content">
					{{item.yuw}}
				</view>
			</view>
		</view>
		<uv-button class="mb-normal" style="width: 100%;" v-if="!loading && total > pageNum * 10" type="success"
			text="查看更多" @click="handleMore"></uv-button>
	</view>
</template>

<style lang="scss" scoped>
	page {
		background-color: #e15c19;
	}

	.uv-nav-slot {
		color: #fff;
	}

	.item {
		background-color: #fff;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		padding: 20rpx;

		.item-time {
			margin-top: 5rpx;
			color: #999;
			font-size: 24rpx;
		}

		.item-content {
			margin-top: 20rpx;
			color: #222;
			font-size: 24rpx;
		}

		.item-more {
			height: 40rpx;
			border-radius: 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #ededed;
			padding: 0 20rpx;
			transition: all .3s;

			&:active {
				opacity: .7;
				transform: scale(.9);
			}
		}
	}
</style>